import java.awt.event.KeyEvent;
import java.util.Random;

//UIUC CS125 FALL 2011 MP. File: PongGame.java, CS125 Project: Challenge2-Hollywood, Version: 2011-09-09T13:38:53-0500.261316470
/**
 * This class provides some diversion - a game of Pong. Feel free to modify it
 * BUT remember that all files must compile without error - otherwise you score
 * 1/100 for the whole MP.
 * 
 * @author angrave && bedoher2 && egroet2
 * 
 */
public class PongGame {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int APPWIDTH = 1000, APPHEIGHT = 800, DELAY = 5;

		int ballWidth = 9, ballHeight = 9;

		int ballX = 200, ballY = 100; // ball starting position

		int dx = 4, dy = 1; // ball velocity

		int bat1X = 100, bat2X = APPWIDTH - bat1X, bat1Y = 20, bat2Y = 30;//Bat coordinates

		int batWidth = 4, batHeight = 100;//Bat dimensions
		
		int player1Score = 0, player2Score = 0;//First and Second Players' Scores
		
		int xCounter = 0, yCounter = 0;//Counters that determine how frequently the speed of the ball is incremented.
		
		int AIResponsiveness = 1;//Sets the initial AI speed

		Zen.create(APPWIDTH, APPHEIGHT, "");//Create window of given dimensions
		
		Random generator = new Random();//Creates a random number generator used for the color creation
		
		Zen.setFont("Courier-48");//Set font to Courier, size 48
		int netDistance = 0;

		while (Zen.isRunning()) {
			bat1Y = Zen.getMouseY();//Sets first player's bat y value to the mouse's y value
			if (ballY > bat2Y && generator.nextInt(5) > 2 )//If ball is below bat, move bat down 1 unit
			{
				bat2Y += AIResponsiveness;
			}
			else if (ballY < bat2Y && generator.nextInt(5) > 2)//If ball is above bat, move bat up 1 unit
			{
				bat2Y -= AIResponsiveness;
			}

			// Don't draw anything above this point -it will get overwritten with black:
			//Zen.setColor(generator.nextInt(255), generator.nextInt(255), generator.nextInt(255));//Generate random color
			Zen.setColor(0,0,0);
			Zen.fillRect(0, 0, APPWIDTH, APPHEIGHT); // Wipe the display area with random color
			
			Zen.setColor(generator.nextInt(255), generator.nextInt(255), generator.nextInt(255));//Generate random color
			// Draw the ball and the two bats
			Zen.fillOval(ballX - ballWidth / 2, ballY - ballHeight / 2,
					ballWidth, ballHeight);
			//Zen.setColor(255, 255, 255); // white
			Zen.setColor(generator.nextInt(255), generator.nextInt(255), generator.nextInt(255));//Generate random color
			Zen.fillRect(bat1X - batWidth / 2, bat1Y - batHeight / 2, batWidth,
					batHeight);
			Zen.setColor(generator.nextInt(255), generator.nextInt(255), generator.nextInt(255));//Generate random color
			Zen.fillRect(bat2X - batWidth / 2, bat2Y - batHeight / 2, batWidth,
					batHeight);
			Zen.setColor(generator.nextInt(255), generator.nextInt(255), generator.nextInt(255));
			Zen.drawText(Integer.toString(player1Score), 200, 50);
			Zen.setColor(generator.nextInt(255), generator.nextInt(255), generator.nextInt(255));
			Zen.drawText(Integer.toString(player2Score), bat2X-100, 50);
			Zen.drawText("LASER PONG", 350, 50);

			// To remove flicker we use double buffering:
			// There's actually two canvas / two screens:
			// The one that is displayed to the user
			// and another behind-the-scenes one that we currently drawing too.
			// When you're finished drawing all of the objects
			// Call flipBuffer and they will swap roles.
			// In practice this also means that you need to clear
			// the contents of the old buffer after calling flipBuffer (see fillRect above)
			Zen.flipBuffer();

			Zen.sleep(DELAY);

			/**
			 * The velocity is negated if the ball hits either of the two
			 * paddles. The velocity components are negated if the ball is at
			 * the edge of the playing area. The ball's position is updated
			 * using the updated velocity state. First we check to see if the X
			 * and Y positions are sufficiently close.
			 */
			boolean hit1 = Math.abs(ballX - bat1X) < (batWidth + ballWidth) / 2
					&& Math.abs(ballY - bat1Y) < (batHeight + ballHeight) / 2;

			boolean hit2 = Math.abs(ballX - bat2X) < (batWidth + ballWidth) / 2
					&& Math.abs(ballY - bat2Y) < (batHeight + ballHeight) / 2;

			if (ballY > APPHEIGHT || ballY < 0)
				dy = -dy;
			
			if (hit1 || hit2)
			{
				if (hit1)
				{
					netDistance = Math.abs(ballY - bat1Y);
					dy = (int)(Math.abs(Math.sin(0.0157 * (double)netDistance)) * dx);
				}
				else if (hit2)
				{
					netDistance = Math.abs(ballY - bat2Y);
					dy = (int)(Math.abs(Math.sin(0.0157 * (double)netDistance)) * dx);
				}
				if (dx > 0)
				{
					if (xCounter%5 == 0)
					{
						dx++;	
					}
					xCounter++;
					dx = -dx;
				}
				else if (dx < 0)
				{
					if (xCounter%5 == 0)
					{
						dx--;	
					}
					xCounter++;
					dx = -dx;
				}
			}

			ballX += dx;
			ballY += dy;
			
			if (ballX > APPWIDTH)
			{
				player1Score++;
				if (player1Score%3 == 0)
				{
					AIResponsiveness++;
				}
				ballX = APPWIDTH/2;
				ballY = APPHEIGHT/2;
				dx = 4;
				dy = 1;
				dx = -dx;
				dy = -dy;
			}
			else if (ballX < 0)
			{
				player2Score++;
				ballX = APPWIDTH/2;
				ballY = APPHEIGHT/2;
				dx = 4;
				dy = 1;
				dx = -dx;
				dy = -dy;
			}
		} // while loop
	} // main method
} // class
